package br.com.vagacerta.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import br.com.vagacerta.modelo.TipoServico;

@Repository
public class TipoServicoDAO {

	private Connection connection;

	@Autowired
	public TipoServicoDAO(DataSource dataSource) {
		try {
			this.connection = dataSource.getConnection();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public TipoServico retornaTipoServicoPorId(long idTipoServico) {
		try {

			String sql = "SELECT * FROM TIPO_SERVICO WHERE COD_SERVICO = ? LIMIT 1";

			PreparedStatement stmt;
			stmt = connection.prepareStatement(sql);

			stmt.setLong(1, idTipoServico);
			ResultSet rs = stmt.executeQuery();
			TipoServico tipoServico = new TipoServico();

			while (rs.next()) {

					tipoServico.setCodigoServico(rs.getLong("COD_SERVICO"));
					tipoServico.setTipoServico(rs.getString("TIPO_SERVICO"));
					tipoServico.setValorServico(rs.getDouble("VALOR"));

			}

			rs.close();
			stmt.close();

			return tipoServico;

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}

	}
	
	
	public List<TipoServico> getListaTiposervico() {

		try {
			String sql = "select * from TIPO_SERVICO WHERE TIPO_SERVICO NOT IN ('HORA')";

			PreparedStatement stmt = connection.prepareStatement(sql);

			ResultSet rs = stmt.executeQuery();
			List<TipoServico> listaTipoServico = new ArrayList<TipoServico>();

			while (rs.next()) {
				TipoServico tipoServico = new TipoServico();
				tipoServico.setCodigoServico(rs.getLong("COD_SERVICO"));
				tipoServico.setTipoServico(rs.getString("TIPO_SERVICO"));
				tipoServico.setValorServico(rs.getDouble("VALOR"));
				listaTipoServico.add(tipoServico);

			}

			rs.close();
			stmt.close();

			return listaTipoServico;

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}

	}

}
